package edu.gznu.ics.fast.pulsar.controller.userController; /**
 * Created by fei on 2016/8/6.
 */


import edu.gznu.ics.fast.pulsar.service.userService.RoleService;
import edu.gznu.ics.fast.pulsar.service.userService.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/user")
public class UserControllerxxxx {

    @Autowired
    private UserService userService;


    @Autowired
    private RoleService roleService;
/*
    @RequiresPermissions("user:view")
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("userList", userService.findAll());
        return "user/list";
    }*/

    /*@RequiresPermissions("user:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        setCommonData(model);
        model.addAttribute("user", new User());
        model.addAttribute("op", "新增");
        return "user/edit";
    }

    @RequiresPermissions("user:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(User user, RedirectAttributes redirectAttributes) {
        userService.createUser(user);
        redirectAttributes.addFlashAttribute("msg", "新增成功");
        return "redirect:/user";
    }

    @RequiresPermissions("user:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        setCommonData(model);
        model.addAttribute("user", userService.findOne(id));
        model.addAttribute("op", "修改");
        return "user/edit";
    }

    @RequiresPermissions("user:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(User user, RedirectAttributes redirectAttributes) {
        userService.updateUser(user);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/user";
    }

    @RequiresPermissions("user:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm(@PathVariable("id") Long id, Model model) {
        setCommonData(model);
        model.addAttribute("user", userService.findOne(id));
        model.addAttribute("op", "删除");
        return "user/edit";
    }

    @RequiresPermissions("user:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        userService.deleteUser(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/user";
    }*/

/*

    @RequiresPermissions("user:update")
    @RequestMapping(value = "/{id}/changePassword", method = RequestMethod.GET)
    public String showChangePasswordForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", userService.findOne(id));
        model.addAttribute("op", "修改密码");
        return "user/changePassword";
    }

    @RequiresPermissions("user:update")
    @RequestMapping(value = "/{id}/changePassword", method = RequestMethod.POST)
    public String changePassword(@PathVariable("id") Long id, String newPassword, RedirectAttributes redirectAttributes) {
        userService.changePassword(id, newPassword);
        redirectAttributes.addFlashAttribute("msg", "修改密码成功");
        return "redirect:/user";
    }
*/

 /*   private void setCommonData(Model model) {
      //  model.addAttribute("organizationList", organizationService.findAll());
        model.addAttribute("roleList", roleService.findAll());
    }*/




/*
    @RequestMapping("/showUser.do")
    public String toIndex(HttpServletRequest request, Model model) {

        int userId = Integer.parseInt(request.getParameter("id"));
        TdUserPO user = this.userService.getUserByName("liuzj");
        model.addAttribute("user", user);
        return "/index";
    }
    @RequestMapping(method = RequestMethod.POST, value = "/saveUser.do",produces="application/json;charset=UTF-8")
   // @RequestMapping()
    @ResponseBody
    public TdUserPO test(@RequestBody List<TdUserPO> tdUserPO,
                         HttpServletResponse response) throws IOException {

int aa=0;
System.out.print(tdUserPO.get(0).getUserName());
        return tdUserPO.get(0);
    }*/
    /*public@ResponseBody
    Map<String,Object> testPostJson(@RequestBody TdUserVO tdUserPO,BindingResult bindingResult){

        Map<String,Object>map=new HashMap<String,Object>();
        if(bindingResult.hasErrors()){
            map.put("errorCode","40001");
            map.put("errorMsg",bindingResult.getFieldError().getDefaultMessage());
        }

        map.put("user",tdUserPO);
        return map;
    }
    @RequestMapping(value="/test",method=RequestMethod.POST)
    public @ResponseBody Map<String, Object> testPostJson(
            @RequestBody  UserForm userForm,
            BindingResult bindingResult) {

        Map<String, Object> map = new HashMap<String, Object>();
        if (bindingResult.hasErrors()) {
            map.put("errorCode", "40001");
            map.put("errorMsg", bindingResult.getFieldError().getDefaultMessage());
        }

        map.put("user", userForm);
        return map;
    }

    private class UserForm {
        public int id,gender;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getGender() {
            return gender;
        }

        public void setGender(int gender) {
            this.gender = gender;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getAccessToken() {
            return accessToken;
        }

        public void setAccessToken(String accessToken) {
            this.accessToken = accessToken;
        }

        public String mobile,password,accessToken;

    }*/
}
